package test;

import common.Utils;
import org.checkerframework.checker.units.qual.A;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.text.SimpleDateFormat;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-09-08
 * Time:11:48
 */
public class DetailPage extends Utils {
    public static String url = "http://81.70.248.108:58080/index.html";
    public DetailPage() {
        super(url);
    }

    /**
     * 检查页面元素
     * @throws InterruptedException
     */
    public void checkDetailPage() throws InterruptedException {
        webDriver.get(url);
        //获取帖子上的用户名
        String author = webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[2]/div/div[1]/ul/li[1]")).getText();
        String articleTitle = webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).getText();
        //页面的跳转
        webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).click();

        //等待页面跳转
        WebDriverWait detailWait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
        detailWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"details_article_title\"]")));
        //获取元素
        String jump_author = webDriver.findElement(By.xpath("//*[@id=\"article_details_author_name\"]")).getText();
        String jump_articleTitle = webDriver.findElement(By.xpath("//*[@id=\"details_article_title\"]")).getText();

        assert author.equals(jump_author):"作者名称不对应";
        assert articleTitle.equals(jump_articleTitle):"帖子标题不对应";
    }


    /**
     * 点赞功能
     */
    public void likeFunction() throws InterruptedException {
        WebDriverWait detailWait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
        //验证点赞功能
        detailWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"details_article_likeCount\"]")));
        String likeCount = webDriver.findElement(By.xpath("//*[@id=\"details_article_likeCount\"]")).getText();
        int originalCount = Integer.parseInt(likeCount);
        System.out.println("原来点赞数"+ originalCount);

        By likeBtn = By.xpath("//*[@id=\"details_btn_like_count\"]");
        WebDriverWait likeBtnWait = new WebDriverWait(webDriver, Duration.ofSeconds(3));
        likeBtnWait.until(ExpectedConditions.elementToBeClickable(likeBtn));
        webDriver.findElement(likeBtn).click();

        //等待1秒
        Thread.sleep(1000);
        String nweLikeCount = webDriver.findElement(By.xpath("//*[@id=\"details_article_likeCount\"]")).getText();
        int newCount = Integer.parseInt(nweLikeCount);
        System.out.println("新点赞数："+nweLikeCount);
        assert newCount-1 == originalCount:"点赞失败";
    }

    public void deleteFunction() throws InterruptedException {
        //获取元素
//        String jump_author = webDriver.findElement(By.xpath("//*[@id=\"article_details_author_name\"]")).getText();
        //*[@id="artical-items-body"]/div[1]/div/div[2]/div[1]/a
//        if (!jump_author.equals("测试用户")){
//            return;
//        }
        WebDriverWait detailWait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
        By deleteBtn = By.xpath("//*[@id=\"bit-forum-content\"]/div[2]/div/div/div[1]/div[2]/div[2]/div/div[3]/div/a");
        detailWait.until(ExpectedConditions.elementToBeClickable(deleteBtn));
        webDriver.findElement(deleteBtn).click();

        //等待是否删除框的的出现
        By certain = By.xpath("//*[@id=\"details_artile_delete\"]");
        By cancel = By.xpath("//*[@id=\"details_delete_modal\"]/div/div/div[3]/div/div/div[1]/a");
        detailWait.until(ExpectedConditions.elementToBeClickable(cancel));
        //1.取消删除
        webDriver.findElement(cancel).click();
        //2.确认删除
        detailWait.until(ExpectedConditions.elementToBeClickable(deleteBtn));
        webDriver.findElement(deleteBtn).click();
        webDriver.findElement(certain).click();
        Thread.sleep(3000);
        //*[@id="details_artile_delete"]
    }
    /**
     * 评论功能
     */

    public void commentFunction() throws InterruptedException {
        WebDriverWait detailWait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
        By comment = By.xpath("//*[@id=\"article_details_reply\"]/div[1]/div[6]/div[1]/div/div/div/div[5]/div/pre");
        By submit = By.xpath("//*[@id=\"details_btn_article_reply\"]");
        detailWait.until(ExpectedConditions.visibilityOfElementLocated(comment));
        WebElement commentEle = webDriver.findElement(comment);
        WebElement submitEle = webDriver.findElement(submit);
        //1. 评论失败
        new Actions(webDriver).click(submitEle).perform();
        //2. 评论成功
        new Actions(webDriver).click(commentEle).sendKeys("自动测试话").perform();
        //延迟一秒
        Thread.sleep(1000);
        new Actions(webDriver).click(submitEle).perform();
    }

    /**
     * 编辑功能
     */
    public void editFunction() throws InterruptedException {
        //获取元素,查看是不是帖子作者
        String jump_author = webDriver.findElement(By.xpath("//*[@id=\"article_details_author_name\"]")).getText();
//        if (!jump_author.equals("测试用户")){
//            return;
//        }
        //获取更新前的标题
        String titleBefore = webDriver.findElement(By.xpath("//*[@id=\"details_article_title\"]")).getText();
        WebDriverWait detailWait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
        detailWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"details_artile_edit\"]")));
        By editBtn = By.xpath("//*[@id=\"details_artile_edit\"]");
        detailWait.until(ExpectedConditions.elementToBeClickable(editBtn));
        webDriver.findElement(editBtn).click();

        //等待页面跳转,查看页面是否编辑界面
        WebDriverWait editWait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
        editWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div/h2")));
        String boardTitle = webDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div/h2")).getText();
        assert boardTitle.equals("编辑");


        //更改标题
        //1.更改失败
        By submit = By.xpath("//*[@id=\"edit_article_submit\"]");
        WebElement submitBtn = webDriver.findElement(submit);

        //1.1 标题为空
        WebElement ele= webDriver.findElement(By.cssSelector("#edit_article_title"));
        new Actions(webDriver).click(ele).perform();
        new Actions(webDriver)
                .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
                .sendKeys(Keys.DELETE)
                .perform();
        new Actions(webDriver).click(submitBtn).perform();
        Thread.sleep(3000);

        //1.2 标题不为空 类容为空
        SimpleDateFormat sim = new SimpleDateFormat("HHmmssSS");
        String titleTime = sim.format(System.currentTimeMillis());
        Thread.sleep(1000);
        WebElement ele1 = webDriver.switchTo().activeElement();
        ele1.sendKeys("自动化11111更新"+titleTime);
        Thread.sleep(3000);
        ele1.sendKeys(Keys.TAB);
        WebElement ele2 = webDriver.switchTo().activeElement();
        new Actions(webDriver)
                .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
                .sendKeys(Keys.DELETE)
                .perform();
        Thread.sleep(3000);
        WebElement submitBtn1 = webDriver.findElement(submit);
        new Actions(webDriver).click(submitBtn1).perform();

        //更新成功
        WebElement ele3= webDriver.findElement(By.cssSelector("#edit_article_title"));
        new Actions(webDriver).click(ele).perform();
        WebElement ele4 = webDriver.switchTo().activeElement();
        ele4.sendKeys(Keys.TAB);
        WebElement ele5 = webDriver.switchTo().activeElement();
        ele5.sendKeys("自动化测试更新类容111111111");
        Thread.sleep(3000);
        WebElement submitBtn2 = webDriver.findElement(submit);
        new Actions(webDriver).click(submitBtn2).perform();
        String titleAfter = webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).getText();
        assert !titleBefore.equals(titleAfter):"更新失败";
        System.out.println("更新成功");

    }
}
